package com.eblly.tomcat;

import com.jfinal.config.*;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.template.Engine;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.core.AprLifecycleListener;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.core.StandardServer;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;

import java.io.File;

/**
 * @author eblly
 * @since 2019/4/1
 */
public class TomcatConfig extends JFinalConfig {

    static Prop prop = PropKit.use("config.properties");

    @Override
    public void configConstant(Constants constants) {

    }

    @Override
    public void configRoute(Routes routes) {
        routes.add("/helloworld", HelloworldController.class);
    }

    @Override
    public void configEngine(Engine engine) {

    }

    @Override
    public void configPlugin(Plugins plugins) {

    }

    @Override
    public void configInterceptor(Interceptors interceptors) {

    }

    @Override
    public void configHandler(Handlers handlers) {

    }

    public static void main(String[] args) throws LifecycleException {
//        int port = Arrays.asList(args).stream()
//                        .filter(s -> s.endsWith("--server.port"))
//                        .findFirst()
//                        .orElse("dev");

        int port = 8888;
        String contextPath = "/";

        // 项目中web目录名称，以前版本为WebRoot、webapp、webapps，现在为WebContent
        String baseDir = new File("webapp/").getAbsolutePath();
        Tomcat tomcat = new Tomcat();
        tomcat.setPort(port);
        tomcat.setBaseDir(".");

        StandardServer server = (StandardServer) tomcat.getServer();
        AprLifecycleListener listener = new AprLifecycleListener();
        server.addLifecycleListener(listener);
        tomcat.addWebapp(contextPath, baseDir);

        tomcat.start();

        tomcat.getServer().await();
    }

}
